package com.cn.juc;

import com.cn.Utils.SleepHelper;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author he_ls
 * @since 2024/11/1 17:48
 **/
public class T02_ThreadState {
    public static void main(String[] args) throws InterruptedException {
         m2();
    }

    public static void m1() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("2nd : " + Thread.currentThread().getState());
            for (int i = 0; i < 3; i++) {
                SleepHelper.sleepSeconds(1);
                System.out.print(i + " ");
            }
            System.out.println();
        });
        System.out.println("1st : " + t1.getState());
        t1.start();
        t1.join();
        System.out.println("3rd :" + t1.getState());
    }

    public static void m2() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("park1");
            LockSupport.park();
            System.out.println("park2");
            SleepHelper.sleepSeconds(5);
        });
        t1.start();
        SleepHelper.sleepSeconds(1);
        System.out.println("4th : " + t1.getState()); //park之后的状态是waiting状态
        LockSupport.unpark(t1);
        TimeUnit.SECONDS.sleep(1);
        System.out.println("5st " + t1.getState());  // 子线程sleep过程中，线程状态是time_waiting
    }

    public static void m3() {
        Object o = new Object();
        Thread t3 = new Thread(() -> {
            synchronized(o) {
                System.out.println("t3 get lock！！");
            }
        });

        new Thread(() -> {
            synchronized (o) {
                System.out.println("someone get lock!!");
                SleepHelper.sleepSeconds(5);
            }
        }).start();

        t3.start();
        SleepHelper.sleepSeconds(1);
        System.out.println("6th : t3 :" + t3.getState());  // 只有synchronized是Blocked，其他的阻塞是waiting状态
    }

    public static void m4() {
        ReentrantLock lock = new ReentrantLock();
        Thread t4 = new Thread(() -> {
            try {
                lock.lock();
                System.out.println("t4 get lock");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println("someone t4 get lock！");
                SleepHelper.sleepSeconds(4);
                System.out.println("someone t4 release lock!");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
        SleepHelper.sleepSeconds(1);
        t4.start();
        SleepHelper.sleepSeconds(1);
        // ReentrantLock 是通过CAS实现的，自旋比较的过程，因此这里获取的不是阻塞而是waiting
        System.out.println("7th : t4 : " + t4.getState()); // waiting

    }

    public static void m5() {

    }

}
